package cn.mall.user.service.impl;

import cn.mall.base.utils.MD5Utils;
import cn.mall.user.domain.CustomerService;
import cn.mall.user.domain.User;
import cn.mall.user.mapper.CustomerServiceMapper;
import cn.mall.user.mapper.UserMapper;
import cn.mall.user.service.ICustomerServiceService;
import cn.mall.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jarvis-Smith
 * @since 2023-12-16
 */
@Service
public class CustomerServiceServiceImpl extends BaseServiceImpl<CustomerService> implements ICustomerServiceService {

    @Autowired
    private CustomerServiceMapper customerServiceMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 新增用户联系客服反馈
     * @param customerService
     */
    @Override
    public void insert(CustomerService customerService) {
        // 参数检验
        // 判断用户名，手机号是否为空
        if (StringUtils.isEmpty(customerService.getUsername()) || StringUtils.isEmpty(customerService.getPhone())) {
            throw new RuntimeException("用户名或手机号不能为空");
        }
        // 查看用户名是否存在
        User user = userMapper.loadByUsername(customerService.getUsername());
        if (Objects.isNull(user)){
            throw new RuntimeException("用户名不存在，请输入你的正确的用户名");
        }
        if (!customerService.getPhone().equals(user.getPhone())){
            throw  new RuntimeException("手机号不正确");
        }
        customerService.setCreatetime(new Date());
        customerServiceMapper.insert(customerService);
    }
}
